#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/11/17 18:39
# @Author  : huidong.bai
# @File    : conftest.py
# @Software: PyCharm


import os
import sys

curPath = os.path.abspath(os.path.dirname(__file__))
rootPath = os.path.split(curPath)[0]
sys.path.append(rootPath)

import threading
import pytest
import constant
from public.common.linuxLogCatchTools import linux_write_log, linux_adb_log_shell
from config import projectPath
from public.common.androidLogCatchTools import android_adb_log_shell, android_write_log
from public.common.rtosLogCatchTools import rtos_write_log


@pytest.fixture(scope="module", autouse=True, name='params')
def choice_command(data):
    print('module前置')
    # 获取音箱的底层系统类型
    system_version = data.get('system_version')
    sn = data.get('sn')
    com = data.get('com')
    command = ''
    try:
        if system_version == constant.LINUX_VERSION:
            # 构造Linux日志抓取命令
            command = linux_adb_log_shell(sn)
        elif system_version == constant.RTOS_VERSION:
            # 构造RTOS日志抓取命令
            command = com
        elif system_version == constant.ANDROID_VERSION:
            # 构造adb logcat安卓抓取命令
            command = android_adb_log_shell(sn)
        else:
            pytest.exit('未知设备类型，请检查：%s' % system_version)
    except Exception as e:
        pytest.exit('未找到日志文件，请检查' % e)
    yield data, command
    print('module后置')


@pytest.fixture(scope="function", autouse=True, name='env_data')
def write_log(params):
    print('function前置')
    data, command = params
    system_version = data.get('system_version')
    thread = None
    log_path = ''
    if system_version == constant.LINUX_VERSION:
        # 创建Linux抓取日志线程
        thread = threading.Thread(target=linux_write_log, args=(command,))
        log_path = projectPath.LINUX_CASH_LOG_DIR
    elif system_version == constant.RTOS_VERSION:
        # 创建RTOS抓取日志线程
        thread = threading.Thread(target=rtos_write_log, args=(command,))
        log_path = projectPath.RTOS_CASH_LOG_DIR
    elif system_version == constant.ANDROID_VERSION:
        # 创建抓取安卓日志线程
        thread = threading.Thread(target=android_write_log, args=(command,))
        log_path = projectPath.ANDROID_CASH_LOG_DIR
    yield data, thread, log_path
    print('function后置')

